define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'entrust/index',
                    table: 'entrust',
                }
            });
            
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                 // 初始化表格
				table1.bootstrapTable({
					url: 'entrust/index',
					queryParams:JSON.stringify({status:1}),
                    toolbar: '#toolbar1',
                    sortName: 'id',
					pk: 'id',
                    search: true,
					fixedColumns:true,   //固定列代码
					fixedRightNumber: 1, 
					columns: [
						[
							{checkbox: true},
							{field: 'id', title: __('Id'), sortable: true},
							{field: 'user.username', title: __('Username'), operate: 'LIKE'},
							{field: 'user.mobile', title: __('Mobile'), operate: 'LIKE'},
							{
								field: 'mode',
								searchable:false, 
								title: __('委托类型'), 
								searchList: {"buy":__('买入'),"sell":__('卖出')},
								formatter:Table.api.formatter.normal,
								custom:{'sell':'yellow','buy':'info'}
							},
							{field: 'num', title: __('委托数量')},
							{field: 'all_no', title: __('股票编码')},
							{field: 'name', title: __('股票名词')},
							{field: 'deal_price', title: __('委托价格')},
							{field: 'createtime', title: __('委托时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', autocomplete:false,searchable:false},
							{
								field: 'is_auto', 
								title: __('自动管理'),
								formatter:Table.api.formatter.normal,
								searchList: {"1":__('是'),"0":__('否')},
							}
						]
					]
				});

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'entrust/index?status=0',
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
						[
							{checkbox: true},
							{field: 'id', title: __('Id'), sortable: true},
							{field: 'username', title: __('Username'), operate: 'LIKE'},
							{field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
							{field: 'mode_text', title: __('委托类型')},
							{field: 'num', title: __('委托数量')},
							{field: 'all_no', title: __('股票编码')},
							{field: 'name', title: __('股票名词')},
							{field: 'deal_time', title: __('委托价格')},
							{field: 'createtime', title: __('委托时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', autocomplete:false,searchable:false},
							{field: 'is_auto_text', title: __('自动管理')},
						]
					]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
			three: function () {
                // 表格2
                var table3 = $("#table3");
                table3.bootstrapTable({
                    url: 'entrust/index?status=2',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar3',
                    sortName: 'id',
                    search: false,
                    columns: [
						[
							{checkbox: true},
							{field: 'id', title: __('Id'), sortable: true},
							{field: 'username', title: __('Username'), operate: 'LIKE'},
							{field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
							{field: 'mode_text', title: __('委托类型')},
							{field: 'num', title: __('委托数量')},
							{field: 'all_no', title: __('股票编码')},
							{field: 'name', title: __('股票名词')},
							{field: 'deal_time', title: __('委托价格')},
							{field: 'createtime', title: __('委托时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', autocomplete:false,searchable:false},
							{field: 'is_auto_text', title: __('自动管理')},
						]
					]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table3);
            }
			
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
			
			events:{
				custom: {
                    'change .btn-browser': function (e, value, row, index) {
                        e.stopPropagation();
                        Layer.alert("该行数据为: <code>" + JSON.stringify(row) + "</code>");
                    }
                },
			}
        }
    };
    return Controller;
});
